package com.example.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.example.bean.ReleaseNoteBean;
import com.example.dao.ReleaseNoteDao;
import com.example.entity.ReleaseNote;
import com.example.service.ReleaseNoteService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("releaseNoteServiceImpl")
@Transactional
public class ReleaseNoteServiceImpl implements ReleaseNoteService {

    @Autowired
    @Qualifier("ReleaseNoteDao")
    ReleaseNoteDao releaseNoteDao;

    @Transactional
    public Long saveReleaseNote(ReleaseNoteBean releaseNoteBean) {
        ReleaseNote releaseNote = releaseNoteBean.toDto();
        releaseNoteDao.saveReleaseNote(releaseNote);
        return releaseNote.getObjId();
    }

    @Transactional
    public ReleaseNoteBean getReleaseNote(Long objId) {
        ReleaseNote releaseNote = releaseNoteDao.getReleaseNote(objId);
        ReleaseNoteBean releaseNoteBean = new ReleaseNoteBean();
        releaseNoteBean.fromDto(releaseNote);
        return releaseNoteBean;
    }

    @Transactional
    public List<ReleaseNoteBean> getAll() {
        List<ReleaseNoteBean> releaseNoteBeans = new ArrayList<ReleaseNoteBean>();
        List<ReleaseNote> releaseNotes = releaseNoteDao.getAllReleaseNotes();
        for (ReleaseNote releaseNote : releaseNotes) {
            ReleaseNoteBean releaseNoteBean = new ReleaseNoteBean();
            releaseNoteBean.fromDto(releaseNote);
            releaseNoteBeans.add(releaseNoteBean);
        }
        Collections.sort(releaseNoteBeans);
        return releaseNoteBeans;
    }

    @Transactional
    public void deleteReleaseNote(Long objId) {
        releaseNoteDao.deleteReleaseNote(objId);
    }
}